package com.huadragon.marathonweb.controller.manage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.huadragon.marathonentity.common.page.PagerModel;
import com.huadragon.marathonentity.servicedomain.MenuInfoDomain;
import com.huadragon.marathonentity.servicedomain.RoleAuthDomain;
import com.huadragon.marathonentity.servicedomain.RoleInfoDomain;
import com.huadragon.marathonservice.service.inter.IMenuInfoService;
import com.huadragon.marathonservice.service.inter.IRoleAuthService;
import com.huadragon.marathonservice.service.inter.IRoleInfoService;
import com.huadragon.marathonweb.common.MenuHelper;
import com.huadragon.marathonweb.common.UserContext;

@Controller
@RequestMapping("role")
public class RoleController {
	@Autowired
	private IRoleInfoService roleInfoService;

	@Autowired
	private IMenuInfoService menuInfoService;
	
	@Autowired
	private IRoleAuthService roleAuthService;
	
	@RequestMapping("list")
	public ModelAndView list() {
		ModelAndView model = new ModelAndView("adminView");
		model.addObject("subtitle", "管理员");
		model.addObject("currenttitle", "角色管理");
		model.addObject("viewPath", "role/list");
		return model;
	}

	@RequestMapping("listdo")
	public ModelAndView listdo(RoleInfoDomain req) {
		ModelAndView model = new ModelAndView("/manage/role/listdo");
		PagerModel pagerModel = roleInfoService.selectPageList(req);
		model.addObject("PagerModel", pagerModel);
		return model;
	}

	@RequestMapping("add")
	@ResponseBody
	public int roleAdd(HttpServletRequest request, String roleName) {
		UserContext context = UserContext.getCurrentContext(request);
		RoleInfoDomain record = new RoleInfoDomain();
		record.setBeLock(false);
		record.setRemark("");
		record.setOptName(context.getUserName());
		record.setRoleName(roleName);
		if(roleInfoService.selectOne(record)!=null)
			return 0;
		return roleInfoService.insert(record);
	}

	@RequestMapping("update")
	@ResponseBody
	public int roleUpdate(HttpServletRequest request, int roleID, int belock, String newName) {
		UserContext context = UserContext.getCurrentContext(request);
		RoleInfoDomain record = new RoleInfoDomain();
		record.setRoleInfoId(roleID);
		record.setOptName(context.getUserName());
		record.setBeLock(belock == 1);
		record.setRoleName(newName);
		return roleInfoService.update(record);
	}

	@RequestMapping(value = "authlist", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String getAuthList(int roleID) {
		List<MenuInfoDomain> menuList = menuInfoService.getMenuListByRoleID(roleID);
		return MenuHelper.getAuthJson(menuList);
	}
	
	@RequestMapping("saveroleauth")
	@ResponseBody
	public String saveRoleAuth(HttpServletRequest request,int roleID, String newAuth, String oldAuth) {
		List<String> newList = new ArrayList<>();
		List<String> oldList = new ArrayList<>();
		List<String> diffList = new ArrayList<>();
		if (newAuth != null && !newAuth.isEmpty()) {
			Collections.addAll(newList, newAuth.split(","));
			Collections.addAll(diffList, newAuth.split(","));
		}
		if (oldAuth != null && !oldAuth.isEmpty()) {
			Collections.addAll(oldList, oldAuth.split(","));
		}

		diffList.removeAll(oldList);// diffList中剩余的是新增的权限id
		oldList.removeAll(newList);// oldList中剩余的是被删掉的权限id
		diffList.addAll(oldList);// diffList中剩余的是发生了变更的权限id
		if (diffList.size() == 0) {
			return "没有任何修改，不需要保存";
		}
		List<RoleAuthDomain> authList = new ArrayList<>();
		UserContext context = UserContext.getCurrentContext(request);
		for (String authid : diffList) {
			RoleAuthDomain authset = new RoleAuthDomain();
			authset.setRoleId(roleID);
			authset.setOptName(context.getUserName());
			authset.setMenuId(Integer.parseInt(authid));
			authList.add(authset);
		}
		roleAuthService.modifyAuthList(authList);

		return "";
	}
}
